LoadingScene = cc.LoaderScene.extend({
    _processLayer: null,
    _processLayerLength: null,
    init: function(){
        cc.Scene.prototype.init.call(this);
        
        //logo
        var logoWidth = config.width;
        var logoHeight = config.height;
        var centerPos = cc.p(this._winSize.width / 2, this._winSize.height / 2);
        
        this._logoTexture = new Image();
        var _this = this;
        this._logoTexture.addEventListener("load", function(){
            _this._initStage(centerPos);
            this.removeEventListener('load', arguments.callee, false);
        });
        this._logoTexture.src = s_start;
        this._logoTexture.width = logoWidth;
        this._logoTexture.height = logoHeight;
        
        // bg
        this._bgLayer = cc.LayerColor.create(cc.c4(0, 0, 0, 255));
        this._bgLayer.setPosition(0, 0);
        this.addChild(this._bgLayer, 0);
        
        this._processLayerLength = config.width * 2 / 3;
        this._processLayer = cc.LayerColor.create(cc.c4b(0, 0, 0, 100), 1, 2);
        this._processLayer.setPosition(cc.pAdd(centerPos, cc.p(-this._processLayerLength / 2, -80)));
        this.addChild(this._processLayer, 10);
        
        //loading percent
        this._label = cc.LabelTTF.create("Loading... 0%", "Arial", 12);
        this._label.setColor(cc.c3(80, 80, 80));
        this._label.setOpacity(0);
        this._label.setPosition(cc.pAdd(centerPos, cc.p(0, -60)));
        this.addChild(this._label, 10);
    },
    _initStage: function(centerPos){
        this._texture2d = new cc.Texture2D();
        this._texture2d.initWithElement(this._logoTexture);
        this._texture2d.handleLoadedTexture();
        this._logo = cc.Sprite.createWithTexture(this._texture2d);
        
        this._logo.setPosition(centerPos);
        this._bgLayer.addChild(this._logo);
        
        //load resources
        this._logoFadeIn();
    },
    _logoFadeIn: function(){
        var logoAction = cc.Spawn.create(cc.FadeIn.create(0.5));
        
        var labelAction = cc.Sequence.create(cc.DelayTime.create(0.15), logoAction.clone());
        
        this._logo.runAction(logoAction);
        this._label.runAction(labelAction);
    },
    _updatePercent: function(){
        var percent = cc.Loader.getInstance().getPercentage();
        var tmpStr = "Loading... " + percent + "%";
        this._label.setString(tmpStr);
        
        this._processLayer.changeWidth(this._processLayerLength * percent / 100);
        
        if (percent >= 100) 
            this.unschedule(this._updatePercent);
    }
});

LoadingScene.preload = function(resources, selector, target){
    if (!this._instance) {
        this._instance = new LoadingScene();
        this._instance.init();
    }
    
    this._instance.initWithResources(resources, selector, target);
    
    var director = cc.Director.getInstance();
    if (director.getRunningScene()) {
        director.replaceScene(this._instance);
    } else {
        director.runWithScene(this._instance);
    }
    
    return this._instance;
};
